package com.kakarote.examine.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kakarote.core.feign.admin.entity.AdminRole;
import com.kakarote.core.feign.admin.entity.SimpleUser;
import com.kakarote.core.feign.admin.service.AdminService;
import com.kakarote.core.feign.examine.entity.ExamineAdvancedConfigLimitTimeUsersVO;
import com.kakarote.core.servlet.BaseServiceImpl;
import com.kakarote.core.utils.UserCacheUtil;
import com.kakarote.examine.entity.PO.Examine;
import com.kakarote.examine.entity.PO.ExamineAdvancedConfigLimitTimeUsers;
import com.kakarote.examine.entity.PO.ExamineRecord;
import com.kakarote.examine.mapper.ExamineAdvancedConfigLimitTimeUsersMapper;
import com.kakarote.examine.service.IExamineAdvancedConfigLimitTimeUsersService;
import com.kakarote.examine.service.IExamineRecordService;
import com.kakarote.examine.service.IExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 审批节点限时处理人员表 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2022-08-30
 */
@Service
public class ExamineAdvancedConfigLimitTimeUsersServiceImpl extends BaseServiceImpl<ExamineAdvancedConfigLimitTimeUsersMapper, ExamineAdvancedConfigLimitTimeUsers> implements IExamineAdvancedConfigLimitTimeUsersService {

    @Autowired
    private IExamineService examineService;
    @Autowired
    private IExamineRecordService examineRecordService;

    @Autowired
    private AdminService adminService;

    /**
     * 根据批次id查询处理人列表
     *
     * @param batchId 批次id
     * @return 处理人列表
     */
    @Override
    public List<ExamineAdvancedConfigLimitTimeUsersVO> queryHandleUserList(String batchId) {
        List<ExamineAdvancedConfigLimitTimeUsersVO> examineAdvancedConfigLimitTimeUsersVOList = new ArrayList<>();
        List<ExamineAdvancedConfigLimitTimeUsers> examineAdvancedConfigLimitTimeUsersList = lambdaQuery()
                .eq(ExamineAdvancedConfigLimitTimeUsers::getBatchId, batchId)
                .orderByAsc(ExamineAdvancedConfigLimitTimeUsers::getSort).list();

        if (CollectionUtil.isNotEmpty(examineAdvancedConfigLimitTimeUsersList)) {
            for (ExamineAdvancedConfigLimitTimeUsers examineAdvancedConfigLimitTimeUsers : examineAdvancedConfigLimitTimeUsersList) {
                ExamineAdvancedConfigLimitTimeUsersVO examineAdvancedConfigLimitTimeUsersVO = new ExamineAdvancedConfigLimitTimeUsersVO();
                examineAdvancedConfigLimitTimeUsersVO.setType(examineAdvancedConfigLimitTimeUsers.getType());
                examineAdvancedConfigLimitTimeUsersVO.setSort(examineAdvancedConfigLimitTimeUsersVO.getSort());

                if (ObjectUtil.isNotEmpty(examineAdvancedConfigLimitTimeUsers.getUserId())) {
                    SimpleUser simpleUser = UserCacheUtil.getSimpleUser(examineAdvancedConfigLimitTimeUsers.getUserId());
                    if (ObjectUtil.isNotEmpty(simpleUser)) {
                        examineAdvancedConfigLimitTimeUsersVO.setUserId(simpleUser.getUserId());
                        examineAdvancedConfigLimitTimeUsersVO.setRealname(simpleUser.getRealname());
                        examineAdvancedConfigLimitTimeUsersVO.setType(0);
                    }
                }

                if (ObjectUtil.equals(examineAdvancedConfigLimitTimeUsers.getType(), 1)) {
                    examineAdvancedConfigLimitTimeUsersVO.setParentLevel(examineAdvancedConfigLimitTimeUsers.getParentLevel());
                }
                if (ObjectUtil.equals(examineAdvancedConfigLimitTimeUsers.getType(), 2)) {
                    AdminRole data = adminService.queryRoleByRoleId(examineAdvancedConfigLimitTimeUsers.getRoleId()).getData();
                    examineAdvancedConfigLimitTimeUsersVO.setRealname(data.getRoleName());
                    examineAdvancedConfigLimitTimeUsersVO.setRoleId(examineAdvancedConfigLimitTimeUsers.getRoleId());
                }
                examineAdvancedConfigLimitTimeUsersVOList.add(examineAdvancedConfigLimitTimeUsersVO);
            }
        }
        return examineAdvancedConfigLimitTimeUsersVOList;
    }

    /**
     * 根据记录Id查询处理人列表
     *
     * @param recordId 记录Id
     * @return 处理人列表
     */
    @Override
    public List<ExamineAdvancedConfigLimitTimeUsers> queryHandleUserListByRecordId(Long recordId) {
        ExamineRecord examineRecord = examineRecordService.getById(recordId);
        Examine examine = examineService.getById(examineRecord.getExamineId());
        return lambdaQuery().eq(ExamineAdvancedConfigLimitTimeUsers::getBatchId, examine.getBatchId()).orderByAsc(ExamineAdvancedConfigLimitTimeUsers::getSort).list();
    }

}
